package com.example.gkcircle.fragment.borrow;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.avos.avoscloud.AVUser;
import com.example.gkcircle.App;
import com.example.gkcircle.BaseFragment;
import com.example.gkcircle.R;
import com.example.gkcircle.activity.BoundLoginActivity;
import com.example.gkcircle.activity.LoginActivity;
import com.example.gkcircle.adapter.BorrowAdapter;
import com.example.gkcircle.adapter.SearchBookAdapter;
import com.example.gkcircle.library.LibraryRequest;
import com.example.gkcircle.library.object.Book;
import com.example.gkcircle.library.object.GKUser;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.MySVProgressHUD;
import com.example.gkcircle.utils.ToastFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 当前借阅Fragment
 * @author ZhouHua
 * created at 15/12/21 上午12:57
 */
public class BorrowingFragment extends BaseFragment{

    private ListView mListView;
    private SwipeRefreshLayout mSwipe;
    private View mHeaderView;
    private View mFooterView;
    private BorrowAdapter mAdapter;
    private boolean isRefreshing;
    private static BorrowingFragment instance;

    public static BorrowingFragment getInstance() {
        if (instance == null) instance = new BorrowingFragment();
        return instance;
    }

    @Override
    protected int setLayout() {
        return R.layout.library_borrowing_fragment;
    }


    @Override
    protected void initView(View view) {
        mListView = (ListView) view.findViewById(R.id.library_borrowing_listview);
        mSwipe = (SwipeRefreshLayout) view.findViewById(R.id.library_borrowing_swipe);
        mSwipe.setColorSchemeColors(getResources().getColor(R.color.main), getResources().getColor(R.color.love), getResources().getColor(R.color.wish));
    }


    @Override
    protected void initListener() {
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!isRefreshing) {
                    isRefreshing = true;
                    getData();
                }
            }
        });
    }

    private void getData(){
        mSwipe.setRefreshing(true);
        LibraryRequest.getInstance().getBorrowing(new LibraryRequest.IGetBorrowingListener() {
            @Override
            public void onSuccess(List<Book> book) {
                Message msg=new Message();
                if(book.size()>0){
                    if(null== App.mBorrowingList){
                        App.mBorrowingList=new ArrayList<Book>();
                    }else{
                        if(isRefreshing)App.mBorrowingList.clear();
                    }
                    for(Book tempBook :book){
                        App.mBorrowingList.add(tempBook);
                    }
                    msg.what=0;
                    handler.sendMessage(msg);
                }else{
                    msg.what=1;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onFailure(int code) {
                Message msg=new Message();
                msg.what=2;
                handler.sendMessage(msg);
            }
        });
    }

    @Override
    protected void setData() {
        if(App.mBorrowingList!=null)return;
        getData();
    }

    private Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    isRefreshing=false;
                    mSwipe.setRefreshing(false);
                    if(!dataFlag)return;
                    if(mAdapter!=null){
                        mAdapter.notifyDataSetChanged();
                        return;
                    }
                    //add headerView and footerView
                    if(mHeaderView==null || mFooterView==null){
                        mHeaderView= LayoutInflater.from(getActivity()).inflate(R.layout.divider_header_footer, null);
                        mFooterView=LayoutInflater.from(getActivity()).inflate(R.layout.divider_header_footer, null);
                        mListView.addHeaderView(mHeaderView, null, false);
                        mListView.addFooterView(mFooterView, null, false);
                    }
                    mAdapter=new BorrowAdapter(getActivity(),App.mBorrowingList);
                    mListView.setAdapter(mAdapter);
                    break;
                case 1:
                    isRefreshing=false;
                    mSwipe.setRefreshing(false);
                    ToastFactory.getToast(getActivity(), getResources().getString(R.string.no_more_data)).show();
                    break;
                case 2:
                    isRefreshing=false;
                    mSwipe.setRefreshing(false);
                    ToastFactory.getToast(getActivity(), getResources().getString(R.string.connection_error)).show();
                    break;
                case 3:
                    setData();
                    break;
            }
        }
    };

    @Override
    public void onPause() {
        mSwipe.setRefreshing(false);
        super.onPause();
    }

}